<?php
namespace app\models;

use yii\db\ActiveRecord;
use Yii;
use app\models\login;

class content extends ActiveRecord{
	public static function tableName(){
		return "{{%content}}";
	}
	
	public function rulues(){
		return[
		    ['content','required','message' => '提交的内容不能为空~'],
			['content','string','max' => 600],
		];
	}
	
	public function add($data){
		if($data){
			//处理性别数据
			$checksex = $data['formSex'];
			if($checksex == '2'){
				$sex = 2;
			} else{
				$sex = 1;
			}
			//创建时间
			date_default_timezone_set('PRC');
			$Createtime = date('Y-m-d H:i:s');
			
			//输入数据库
			$this->content = $data['formContent'];
			$this->nickname = $data['formName'];
			$this->sex = $sex;
			$this->createtime = $Createtime;
                        $this->likecount = 0;
			$this->openid = $data['username'];
			$result = $this->save();
			if($result){
				$error = 0;
				return $error;
			}else{
				$error = 2;
				return $error;
		   }
	}	
}
		
	public function mod($id,$include){
			//评论数增加
			$condition = [
			       'content_id' => $id,
				];
		    $keys = array_keys($include);
			if($keys[0] == 'comment'){
				$data = [
				'count_p' => $include['comment']
				];
			}else if($keys[0] == 'likecount'){
				$data = [
				     'likecount' =>$include['likecount']
					 ];
			}
				$result = self::updateAll($data,$condition);
				return $result;
            }

          //删除内容操作:把state改为0不显示出即可
          public function del($data){
                    if($data){
                 $content_id = $data['formid'];
                 $sql = "update wall_content set state = 0 where content_id ='$content_id'";
                 $State = content::findOne($content_id);
                 $State->state= '0';
                 $result = $State->save();
                 if($result){
                        $error = 0;
                        return $error;
                 }else{
                        $error = $this->getErrors();
                        return $error;
                  }
                }else{
                        $error = 2;
                        return $error;
                }
            }	

	}			

